<?php

namespace Yj\Generator;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Config\Config;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Yj\Generator\Concerns\interactsWithIO;
use Yj\Generator\Core\Generator;

#[Command]
class ModelCommand extends HyperfCommand
{
    use interactsWithIO;

    protected $name = 'create:model';

    /**
     * @var Generator
     */
    protected $generator;

    public function __construct(Generator $generator)
    {
        parent::__construct();
        $this->generator = $generator;
    }

    public function handle()
    {
        //生成配置文件
        $config = $this->createConfig();

        $model = $this->generator->generateModel($config);
        if ($config->get('preview') == true) {
            $this->output->writeln($model->render());
        } else {
            $this->output->writeln(sprintf('Model %s generated', $model->getName()->getName()));
        }

    }

    public function createConfig()
    {
        $config = [];
        foreach ($this->getArguments() as $argument) {
            $config[$argument[0]] = $this->argument($argument[0]);
        }
        foreach ($this->getOptions() as $option) {
            $value = $this->option($option[0]);
            if ($option[2] == InputOption::VALUE_NONE && $value === false) {
                $value = null;
            }
            $config[$option[0]] = $value;
        }

        return new \Yj\Generator\Core\Config($config);
    }

    public function register()
    {
//        $this->app->tag([
//            ExistenceCheckerProcessor::class,
//            FieldProcessor::class,
//            NamespaceProcessor::class,
//            RelationProcessor::class,
//            CustomPropertyProcessor::class,
//            TableNameProcessor::class,
//            CustomPrimaryKeyProcessor::class,
//        ], self::PROCESSOR_TAG);
//
//        $this->app->bind(EloquentModelBuilder::class, function ($app) {
//            return new EloquentModelBuilder($app->tagged(self::PROCESSOR_TAG));
//        });
    }

    protected function getArguments()
    {
        return [
            ['class_name', InputArgument::REQUIRED, 'Model class name'],
            ['dirname', InputArgument::OPTIONAL, 'dirname'],
            ['preview', InputArgument::OPTIONAL, 'dirname'],
        ];
    }

    protected function getOptions()
    {
        return [
            ['table-name', 'tn', InputOption::VALUE_OPTIONAL, 'Name of the table to use', null],
            ['output-path', 'op', InputOption::VALUE_OPTIONAL, 'Directory to store generated model', null],
            ['namespace', 'ns', InputOption::VALUE_OPTIONAL, 'Namespace of the model', null],
            ['base-class-name', 'bc', InputOption::VALUE_OPTIONAL, 'Model parent class', null],
            ['no-timestamps', 'ts', InputOption::VALUE_NONE, 'Set timestamps property to false', null],
            ['date-format', 'df', InputOption::VALUE_OPTIONAL, 'dateFormat property', null],
            ['connection', 'cn', InputOption::VALUE_OPTIONAL, 'Connection property', null],
            ['backup', 'b', InputOption::VALUE_NONE, 'Backup existing model', null]
        ];
    }

}